//
//  AnimationFactory.m
//  GouPiao
//
//  Created by huajian zhou on 12-4-27.
//  Copyright (c) 2012年 itotemstudio. All rights reserved.
//

#import "AnimationFactory.h"

@implementation AnimationFactory
+ (CAKeyframeAnimation*) scaleAnimation:(BOOL)show
{
    CAKeyframeAnimation *scaleAnimation = nil; 
    scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 
//    scaleAnimation.delegate = self;
    scaleAnimation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    if (show) 
    {
        scaleAnimation.duration = 0.5; 
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; 
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]]; 
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];         
    }
    else
    {
        scaleAnimation.duration = 0.3;         
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 0.9)]]; 
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];                 
        [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.6, 0.6, 1.0)]];                         
    }
    scaleAnimation.values = values;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];     
    scaleAnimation.removedOnCompletion = TRUE;
    return scaleAnimation;
}

+ (void) addMarqueeAnimationForLabel:(UILabel *)label
{
    [label.layer removeAllAnimations];
    label.left = 0;
    
    CGSize boundingSize = CGSizeMake(CGFLOAT_MAX, 20);
    NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: label.font, NSFontAttributeName, nil];
    CGFloat oldLabelWidth = [label.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.height;
    
    if (oldLabelWidth<=label.superview.width) {
        return;
    }
    CGFloat moveWidth = [[NSString stringWithFormat:@"%@     ",label.text] boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    
    label.text = [NSString stringWithFormat:@"%@     %@",label.text,label.text];
    CGFloat labelWidth = [label.text boundingRectWithSize:boundingSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    
    label.width = labelWidth;
    
    [UIView animateWithDuration:label.width/60 delay:0.5 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionOverrideInheritedDuration animations:^
     {
         label.left  = -moveWidth+1;            
     } completion:^(BOOL finished)
     {      
         if (finished)
         {
             label.left = 0;             
         }
     }]; 
}
@end
